package panda.transaction.recovery;

import panda.buffer.Buffer;
import panda.buffer.BufferManager;
import panda.file.Block;
import panda.log.BasicLogRecord;
import panda.server.Panda;

public class SetArrayRecord implements LogRecord {
	private int transactionNumber, offset;

	byte[] value;

	private Block block;

	public SetArrayRecord(int transactionNumber, Block block, int offset,
			byte[] value) {
		this.transactionNumber = transactionNumber;
		this.block = block;
		this.offset = offset;
		this.value = new byte[value.length];
		this.value = value.clone();
	}

	public SetArrayRecord(BasicLogRecord record) {
		transactionNumber = record.getInt();
		String filename = record.getString();
		int blockNumber = record.getInt();
		block = new Block(filename, blockNumber);
		offset = record.getInt();
		value = record.getArray();
	}

	public int getTransactionNumber() {
		return transactionNumber;
	}

	public int getType() {
		return SETARRAY;
	}

	public void undo(int transactionNumber) {
		BufferManager bufferManager = Panda.getBufferManager();
		Buffer buffer = bufferManager.pin(block);
		buffer.putArray(offset, value, transactionNumber, -1);
		bufferManager.unpin(buffer);

	}

	public int writeToLog() {
		return logManager.append(new Object[] { SETARRAY, transactionNumber,
				block.getFileName(), block.getBlockNumber(), offset, value });
	}

}
